﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FineUI;
using ZHDJ.Core;
using ZHDJ.Core.Util;
using ZHDJ.Data.BLL;
using ZHDJ.Data.Models.Center;
using ZHDJ.WebPage;
using ZHDJ.WebPage.Master;

namespace ZHDJ.Web.Portal.Possy
{
    public partial class PossyAppointment : UseMorePageMasterPage
    {
        public string PossyID
        {
            get { return Request.QueryString["PossyID"] ?? ""; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            CheckLogin(true);
            if (!IsPostBack)
            {
                BindData();
                LoadData();
            }
        }

        public void BindData()
        {
            ControlBusiness.ControlBindDataDictionary(this.ddlVisitReason, SysDataDictionary.PossyVisitReason);
            ControlBusiness.ControlBind(this.ddlPossy, PartyPossyInfo.GetGridData(0,int.MaxValue,"1",""), "Name", "ID");
            var pickerurl = String.Format("../../app/Share/Picker/BranchSinglePicker.aspx?ids=<script>{0}</script>",
                hdBranchID.GetValueReference());
            ppApplyBranch.OnClientTrigger2Click = Window1.GetSaveStateReference(hdBranchID.ClientID, ppApplyBranch.ClientID)
                                              + Window1.GetShowReference(pickerurl);
            //清空脚本
            StringBuilder js = new StringBuilder();
            js.AppendFormat(@"
                ; var pickerClientID = '{0}';
                  var pickerHdClientID = '{1}';
                  F(pickerClientID).setValue('');
                  F(pickerHdClientID).setValue('');
", ppApplyBranch.ClientID, hdBranchID.ClientID);
            ppApplyBranch.OnClientTrigger1Click = js.ToString();
        }

        public void LoadData()
        {
            txtPossyBranch.Readonly = false;
            ddlPossy.Readonly = false;
            if (!string.IsNullOrEmpty(PossyID))
            {
                this.ddlPossy.SelectedValue = PossyID;
               
                ddlPossy.Readonly = true;
            }
            LoadPossyBranch();
            txtPossyBranch.Readonly = true;
            this.ppApplyBranch.Text= ControlBusiness.GetBranchPickerText(CurrentUser.BranchID);
            this.hdBranchID.Text = CurrentUser.BranchID;
            this.txtUserName.Text = CurrentUser.UserName;
            this.txtMobileNo.Text = CurrentUser.MobileNo;
            this.dtAppointmentTime.Text = DateTime.Now.ToString("yyyy-MM-dd");
            this.dtAppointmentTime.MinDate = DateTime.Now;
        }

        public bool CheckValue()
        {
           
            var rs = true;
            if (this.ppApplyBranch.Text.Trim() == "" || hdBranchID.Text.Trim() == "")
            {
                this.ppApplyBranch.MarkInvalid("必填");
                rs = false;
            }
            if (this.txtUserName.Text.Trim() == "")
            {
                this.txtUserName.MarkInvalid("必填");
                rs = false;
            }
            if (this.dtAppointmentTime.Text.Trim() == "")
            {
                this.dtAppointmentTime.MarkInvalid("必填");
                rs = false;
            }
            if (this.ddlAppointmentTimeSpen.SelectedValue.Trim() == "")
            {
                this.ddlAppointmentTimeSpen.MarkInvalid("必填");
                rs = false;
            }
            if (this.ddlVisitReason.SelectedValue.Trim() == "")
            {
                this.ddlVisitReason.MarkInvalid("必填");
                rs = false;
            }
            if (this.txtPeopleCount.Text.Trim() == "")
            {
                this.txtPeopleCount.MarkInvalid("必填");
                rs = false;
            }
            if (this.txtUserName.Text.Trim() == "")
            {
                this.txtUserName.MarkInvalid("必填");
                rs = false;
            }

            if (this.txtMobileNo.Text.Trim() == "")
            {
                this.txtMobileNo.MarkInvalid("必填");
                rs = false;
            }
            if (this.txtLeader.Text.Trim() == "")
            {
                this.txtLeader.MarkInvalid("必填");
                rs = false;
            }
            if (this.txtLeaderPosition.Text.Trim() == "")
            {
                this.txtLeaderPosition.MarkInvalid("必填");
                rs = false;
            }
            if (this.txtDesc.Text.Trim() == "")
            {
                this.txtDesc.MarkInvalid("必填");
                rs = false;
            }
            return rs;
        }

        public void SaveData()
        {
            if (!CheckValue())
                return;
            var appoitments = new PartyPossyAppointment();
            appoitments.ApplyBranch= this.hdBranchID.Text;
            appoitments.AppointmentTime = Convert.ToDateTime(dtAppointmentTime.Text);
            appoitments.AppointmentTimeSpen = ddlAppointmentTimeSpen.SelectedValue;
            appoitments.BranchID = CurrentUser.BranchID;
            appoitments.Describe = txtDesc.Text;
            appoitments.ID = StringHelper.NewID;
            appoitments.Leader = txtLeader.Text;
            appoitments.LeaderPosition = txtLeaderPosition.Text;
            appoitments.MobileNo = txtMobileNo.Text;
            appoitments.PeopleCount = Convert.ToInt32(txtPeopleCount.Text);
            appoitments.PossyID = this.ddlPossy.SelectedValue;
            appoitments.Status = 0;
            appoitments.UserName = txtUserName.Text;
            appoitments.VisitReason = ddlVisitReason.SelectedValue;
            appoitments.CreateBy = CurrentUser.LoginID;
            appoitments.ModifyBy = CurrentUser.LoginID;
            appoitments.ModifyTime = DateTime.Now;
            appoitments.CreateTime = DateTime.Now;
            DB.PartyPossyAppointments.Add(appoitments);
            if (DB.SaveChanges() > 0)
            {
                PageContext.RegisterStartupScript(FineUI.Alert.GetShowReference("预约信息已提交。", "提示","su();"));
                //添加待办
                PartyTaskHelper.AddTaskToSysMamage(appoitments.ID, "预约党建阵地", SysEnum.TaskCategory.Possy,
                    appoitments.CreateBy,
                    "../../../../App/SysManage/Module/Possy/PartyPossyAppointmentEdit.aspx?id=" + appoitments.ID);
            }
            else
            {
                FineUI.Alert.Show( "提交失败，请稍后再试。");
            }
        }
        public void LoadPossyBranch()
        {
            var possy = DB.PartyPossyInfos.Find(this.ddlPossy.SelectedValue);
            if (possy != null)
            {
                txtPossyBranch.Text = ControlBusiness.GetBranchPickerText(possy.BranchID);
            }
        }

        #region 模块菜单
        /// <summary>
        /// 初始化module  如果通过url传参可不设置
        /// </summary>
        protected override void InitModule()
        {
            Module = "PartyPossy";
            //Category = "";

        }
        #endregion

        protected void btnSave_Click(object sender, EventArgs e)
        {
            SaveData();
        }

        protected void ddlPossy_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadPossyBranch();
        }
    }
}